package com.dsusoft.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dsusoft.config.AppSettings;

import common.BMP2JPEG;
import common.DICOM2JPEG;
import common.HttpUtil;
import common.ZipUtil;

@RequestMapping("image")
@Controller
public class DICOMImageCtrl {

    @Autowired
    private AppSettings appSettings;

    @RequestMapping(value = "DICOM2JPEG", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String test1(String path) {
        String pathArr[] = path.split(";");
        StringBuffer outPath = new StringBuffer();
        for (String str : pathArr) {
            if (str.indexOf(":\\") < 1) {
                str = appSettings.getPicdz() + str;
            }
            try {
                outPath.append(DICOM2JPEG.dicomToJpeg(str));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return outPath.toString();
    }

    @RequestMapping(value = "ZIPDICOM2JPEG", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String test2(String path) {
        String pathArr1[] = path.split(";");
        StringBuffer outpath1 = new StringBuffer();
        for (String str : pathArr1) {
            if (str.indexOf(":\\") < 1) {
                str = appSettings.getPicdz() + str;
            }
            try {
                outpath1.append(ZipUtil.unzip(str));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String pathArr2[] = outpath1.toString().split(";");
        StringBuffer outPath2 = new StringBuffer();
        for (String str : pathArr2) {
            if (str.indexOf(":\\") < 1) {
                str = appSettings.getPicdz() + str;
            }
            try {
                outPath2.append(DICOM2JPEG.dicomToJpeg(str));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return outPath2.toString();
    }

    @RequestMapping(value = "BMP2JPEG", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String test3(String path) {
        String pathArr[] = path.split(";");
        StringBuffer outPath = new StringBuffer();
        for (String str : pathArr) {
            if (str.indexOf(":\\") < 1) {
                str = appSettings.getPicdz() + str;
            }
            try {
                outPath.append(BMP2JPEG.bmpToJpeg(str));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return outPath.toString();
    }

    @RequestMapping(value = "BMP2JPEGROTATE", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String test3(String path, int angle) {
        String pathArr[] = path.split(";");
        StringBuffer outPath = new StringBuffer();
        for (String str : pathArr) {
            if (str.indexOf(":\\") < 1) {
                str = appSettings.getPicdz() + str;
            }
            try {
                outPath.append(BMP2JPEG.bmpToJpeg(str, angle));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return outPath.toString();
    }

    @RequestMapping(value = "DOWNLOAD", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String test4(String url, String filepath) {
        String path = null;
        try {
            path = HttpUtil.download(url, filepath);
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
        return "1";
    }
}
